import { CLOUD_JS_BRIDGE_REGISTER_HANDLER, CLOUD_WEBVIEW_URL, ICallbackVoid, getStatusBarColor } from "../tokens/const";
import { tryGet } from "../utils/tryGet";
import { parseJson } from "../utils/parseJson";
import { tryGetAll } from "../utils/tryGetAll";

export function layoutInitilation() {
    log(`主UI初始化...`)
    const statusBarColor = getStatusBarColor()
    ui.statusBarColor(statusBarColor)
    ui.layout(`<frame w="*" h="*">
    <webview id="main" />
</frame>` as any)
    log(`主界面渲染完成`)
    const main: any = ui.findView('main')
    if (main) {
        const cloudUrl = tryGet(CLOUD_WEBVIEW_URL)
        main.loadUrl(cloudUrl)
        const registerHandlers = tryGetAll(CLOUD_JS_BRIDGE_REGISTER_HANDLER)
        registerHandlers.map((payload) => {
            main.jsBridge.registerHandler(payload.type, (data: any, callBack: ICallbackVoid) => {
                const parseVal = parseJson(data)
                callBack(payload.payload(parseVal))
            });
        })
    } else {
        log(`主界面尚未渲染`)
    }
}